#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2022/4/18 12:59
# @Author  : Samge
import requests
import json

DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'


def get_token(account, password):
    """
    获取登录后的token信息
    :param account:
    :param password:
    :return:
    """
    if not account or not password:
        raise ValueError("请输入正确的账号跟密码")
    url: str = 'https://www.itkz.net/api/login'
    post_dict: dict = {"loginType": 1, "account": f"{account}", "password": f"{password}"}
    headers: dict = {
        'user-agent': DEFAULT_USER_AGENT,
        'content-type': 'application/json;charset=UTF-8'
    }
    print(f'登录信息：{url}\n{post_dict}\n{headers}')
    r = requests.post(
        url=url,
        headers=headers,
        data=json.dumps(post_dict).encode('utf-8')
    )

    ret = eval(r.text)
    if ret.get('code') != 200 or not ret.get('data').get('token'):
        raise ValueError(f"获取token失败 token_util 【{ret.get('message')}】")
    return ret.get('data').get('token')
